package com.ruoyi.tester.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.json.JSONObject;
import com.ruoyi.tester.executor.expression.ExpressionContainer;
import com.ruoyi.tester.service.ITesterExecutorService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tester.domain.TesterInterface;
import com.ruoyi.tester.service.ITesterInterfaceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接口信息Controller
 * 
 * @author SJ
 * @date 2022-04-22
 */
@Controller
@RequestMapping("/tester/task_interface")
public class TesterInterfaceController extends BaseController
{
    private String prefix = "tester/task_interface";

    @Autowired
    private ITesterInterfaceService testerInterfaceService;

    @Autowired
    private ITesterExecutorService testerExecutorService;

    @RequiresPermissions("tester:task_interface:view")
    @GetMapping()
    public String task_interface()
    {
        return prefix + "/task_interface";
    }

    /**
     * 查询接口信息列表
     */
    @ApiOperation("查询接口信息列表")
    @RequiresPermissions("tester:task_interface:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TesterInterface testerInterface)
    {
        startPage();
        List<TesterInterface> list = testerInterfaceService.selectTesterInterfaceList(testerInterface);
        return getDataTable(list);
    }

    /**
     * 导出接口信息列表
     */
    @RequiresPermissions("tester:task_interface:export")
    @Log(title = "接口信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TesterInterface testerInterface)
    {
        List<TesterInterface> list = testerInterfaceService.selectTesterInterfaceList(testerInterface);
        ExcelUtil<TesterInterface> util = new ExcelUtil<TesterInterface>(TesterInterface.class);
        return util.exportExcel(list, "接口信息数据");
    }

    /**
     * 新增接口信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<String> expDescList = ExpressionContainer.expressionsDescription();
        String expDesc = expDescList.stream().collect(Collectors.joining("\r\n"));
        mmap.put("expDesc", expDesc);

        List<String> expNameList = ExpressionContainer.expressionsName();
        String expNames = expNameList.stream().map(x -> x+"()").collect(Collectors.joining(","));
        mmap.put("expNames", expNames);
        return prefix + "/add";
    }

    /**
     * 新增保存接口信息
     */
    @ApiOperation("新增保存接口信息")
    @RequiresPermissions("tester:task_interface:add")
    @Log(title = "接口信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TesterInterface testerInterface)
    {
        testerInterfaceService.insertTesterInterface(testerInterface);
        return success(testerInterface.getInterfaceId());
    }

    /**
     * 修改接口信息
     */
    @RequiresPermissions("tester:task_interface:edit")
    @GetMapping("/edit/{interfaceId}")
    public String edit(@PathVariable("interfaceId") Long interfaceId, ModelMap mmap)
    {
        TesterInterface testerInterface = testerInterfaceService.selectTesterInterfaceByInterfaceId(interfaceId);
        mmap.put("testerInterface", testerInterface);

        List<String> expDescList = ExpressionContainer.expressionsDescription();
        String expDesc = expDescList.stream().collect(Collectors.joining("\r\n"));
        mmap.put("expDesc", expDesc);

        List<String> expNameList = ExpressionContainer.expressionsName();
        String expNames = expNameList.stream().map(x -> x+"()").collect(Collectors.joining(","));
        mmap.put("expNames", expNames);
        return prefix + "/edit";
    }

    /**
     * 修改保存接口信息
     */
    @ApiOperation("修改保存接口信息")
    @RequiresPermissions("tester:task_interface:edit")
    @Log(title = "接口信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TesterInterface testerInterface)
    {
        return toAjax(testerInterfaceService.updateTesterInterface(testerInterface));
    }

    /**
     * 删除接口信息
     */
    @ApiOperation("删除接口信息")
    @RequiresPermissions("tester:task_interface:remove")
    @Log(title = "接口信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(testerInterfaceService.deleteTesterInterfaceByInterfaceIds(ids));
    }

    @RequiresPermissions("tester:task_interface:swagger_import")
    @GetMapping("/swagger_import")
    public String swaggerImport()
    {
        return prefix + "/swagger_import";
    }

    /**
     * 查询swagger接口信息
     */
    @ApiOperation("查询swagger接口信息")
    @RequiresPermissions("tester:task_interface:swagger_import")
    @PostMapping("/swagger_import")
    @ResponseBody
    public TableDataInfo swaggerSearch(String swaggerResourcesUrl)
    {
        logger.info("swaggerResourcesUrl={}", swaggerResourcesUrl);
        List<TesterInterface> list = testerExecutorService.swaggerSearch(swaggerResourcesUrl);
        return getDataTable(list);
    }

    @RequiresPermissions("tester:task_interface:swagger_import")
    @Log(title = "保存swagger", businessType = BusinessType.INSERT)
    @PostMapping( "/saveSwaggerInterface")
    @ResponseBody
    public AjaxResult saveSwaggerInterface(@RequestBody List<TesterInterface> list)
    {
        list.stream().forEach(x -> {
            if(x.getMapping().charAt(0) != '/'){
                x.setMapping("/" + x.getMapping());
            }
            if(x.getMapping() != null && x.getMapping().contains("//")){
                x.setMapping(x.getMapping().replace("//", "/"));
            }
        });
        return toAjax(testerInterfaceService.batchInsertTesterInterface(list));
    }
}
